<!-- Designed and Developed by VSE Group -->
<!-- For any concerns :  gaurav.pruthi@tcs.com -->

<?php
session_start(); 
ini_set('display_errors', 1);
ini_set('error_reporting', E_ERROR);
?>					

<?php include('./classes/mailer.php'); ?>
<?php include('./classes/employee.php'); ?>
<?php 
require_once('KLogger.php');
$log = new KLogger ( "log.txt" , KLogger::DEBUG );
?>




<?php
$username = $_SESSION['username'];
$id = $_SESSION['id'];
$role = $_SESSION['role'];

// Connecting to database 

$con = mysql_connect("localhost","root","");
mysql_select_db("checklist" , $con);

$check = "select designer ,internal_idce_reviewer,external_idce_reviewer,checklist_reviewer,pl,dp_prime from cr where id = '$id' ";
$result = mysql_query($check) or die(mysql_error());
$data = mysql_fetch_assoc($result);
$internal_idce_reviewer = $data['internal_idce_reviewer'];
$checklist_reviewer = $data['checklist_reviewer'];
$external_idce_reviewer = $data['external_idce_reviewer'];
$pl = $data['pl'];
$dp_prime = $data['dp_prime'];
$designer = $data['designer'];


if($username==null || $username== ""|| $username == "")
{
	header('Location: http://172.26.195.159/checklist/php/logout.php');
}
?>


<?php

//Retrieving Data

$d_response1 = $_REQUEST['d_response1'];
$d_response2 = $_REQUEST['d_response2'];
$d_response3 = $_REQUEST['d_response3'];
$d_response4 = $_REQUEST['d_response4'];
$d_response5 = $_REQUEST['d_response5'];
$d_response6 = $_REQUEST['d_response6'];

$d_response = array($d_response1 , $d_response2 , $d_response3 , $d_response4 ,$d_response5 ,$d_response6);

$r_response1 = $_REQUEST['r_response1'];
$r_response2 = $_REQUEST['r_response2'];
$r_response3 = $_REQUEST['r_response3'];
$r_response4 = $_REQUEST['r_response4'];
$r_response5 = $_REQUEST['r_response5'];
$r_response6 = $_REQUEST['r_response6'];

$r_response = array($r_response1 , $r_response2 , $r_response3 , $r_response4 ,$r_response5 ,$r_response6);

$d_comments1 = $_REQUEST['d_comments1'];
$d_comments2 = $_REQUEST['d_comments2'];
$d_comments3 = $_REQUEST['d_comments3'];
$d_comments4 = $_REQUEST['d_comments4'];
$d_comments5 = $_REQUEST['d_comments5'];
$d_comments6 = $_REQUEST['d_comments6'];

$d_comments = array($d_comments1 , $d_comments2 , $d_comments3 , $d_comments4 ,$d_comments5 ,$d_comments6);

$r_comments1 = $_REQUEST['r_comments1'];
$r_comments2 = $_REQUEST['r_comments2'];
$r_comments3 = $_REQUEST['r_comments3'];
$r_comments4 = $_REQUEST['r_comments4'];
$r_comments5 = $_REQUEST['r_comments5'];
$r_comments6 = $_REQUEST['r_comments6'];

$r_comments = array($r_comments1 , $r_comments2 , $r_comments3 , $r_comments4 ,$r_comments5 ,$r_comments6);

//Checking if record for id already exists....
$check = "select * from external_idce where id = '$id' ";
$result = mysql_query($check);
if(mysql_num_rows($result))
{
  
  if($username == $designer)
  {
  for ($i=0; $i<=5; $i++)
  {
  $sql2 = "update external_idce set designer_response='$d_response[$i]' , designer_comments='$d_comments[$i]' where id='$id' AND serial=($i+1)";
  mysql_query($sql2) or die(mysql_error());
  }
  }
  if($username == $external_idce_reviewer)
  {
  for ($i=0; $i<=5; $i++)
  {
  $sql3 = "update external_idce set reviewer_response='$r_response[$i]' , reviewer_comments='$r_comments[$i]' where id='$id' AND serial=($i+1)";
  mysql_query($sql3) or die(mysql_error());
  }
  } //End of loop....
  
echo '<script type="text/javascript">','alert("Data Successfully fed...redirecting to mainpage!!!");',
'window.location = "http://172.26.195.159/checklist/php/mainpage.php";' ,'</script>';
}

//If not exits then submit

else
{

for ($i=0; $i<=5; $i++)
  {
  $sql = "insert into external_idce values('$id' , ($i+1) , '$d_response[$i]' , '$d_comments[$i]' , '$r_response[$i]' , '$r_comments[$i]')";
  mysql_query($sql) or die(mysql_error());
  }
  //updating status
  $sql2 = "update cr_status set status=5 where id ='$id'";
  mysql_query($sql2) or die(mysql_error());
  /*********************************Send Mail***************************************************/

  $log->logInfo('processExternalIDCE[130] , Starting mail : '.$mail_designer);
$m = new mailer();

//Gathering data....
$sql = "select * from cr where id = '$id' ";
$result = mysql_query($sql);
$data_from_cr_for_mail = mysql_fetch_array($result);

//Feeding Data for CR..
$m->set_id($id);
$m->set_title($data_from_cr_for_mail['title']);
$m->set_status(3);

//Fetching designer's mailing address details....
$query = "SELECT email FROM employees WHERE username in(SELECT designer FROM cr where id = '$id')"; 
$result = mysql_query($query);
$mail_designer_data = mysql_fetch_array($result); 

$mail_designer = $mail_designer_data['email'];  

$log->logInfo('Designer email address : '.$id.'ok');
                     //got the mail of designer...
mysql_free_result($result);                                         //freeing the result

//Fetching reviewer's mailing address details..
//fetching usernames.
$query = "select checklist_reviewer,internal_idce_reviewer,external_idce_reviewer,pl,dp_prime from cr_status where id='$id'";
$result = mysql_query($query);
$reviewers_names = mysql_fetch_array($result);                       
$checklist_reviewer = $reviewers_names['checklist_reviewer'];
$internal_idce_reviewer = $reviewers_names['internal_idce_reviewer'];
$external_idce_reviewer = $reviewers_names['external_idce_reviewer'];
$dp_prime = $reviewers_names['dp_prime'];
$pl = $reviewers_names['pl'];

//fetching mail_addresses..
$query = "select email from employees where username = '$checklist_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);  
$mail_checklist_reviewer = $mail_reviewers['email'];

$query = "select email from employees where username = '$external_idce_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_external_idce_reviewer = $mail_reviewers['email'];

$query = "select email from employees where username = '$pl' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_pl = $mail_reviewers['email'];

$query = "select email from employees where username = '$dp_prime' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_dp_prime = $mail_reviewers['email'];

$query = "select email from employees where username = '$internal_idce_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_internal_idce_reviewer = $mail_reviewers['email'];

// '$checklist_reviewer' , '$internal_idce_reviewer','$external_idce_reviewer','$pl','$dp_prime'

$cc = array();
$cc[0]=$mail_designer;
$cc[1]=$mail_checklist_reviewer;
$cc[2]=$mail_internal_idce_reviewer ;
$cc[3]=$mail_pl;
$cc[4]=$mail_dp_prime;


$log->logInfo('processExternalIDCE[200] , $mail_designer  : '.$mail_designer);
$log->logInfo('processExternalIDCE[201] , $mail_checklist_reviewer :'.$mail_checklist_reviewer);

//Setting to and ccs'
$m->set_to($mail_external_idce_reviewer);

//Setting message and body..
$m->set_headers('From: "WebChecklist Notification" <WebChecklist_Message_Broker@tcs.com>'."\r\n".
"CC : ".$cc[0]." , ".$cc[1]." , ".$cc[2]." , ".$cc[3]." , ".$cc[4]);


$m->set_subject("Status of CR : ".$id." has been changed to ".getStatus(3)."");
$m->set_body("Hi , "."\n\n"."The External Review of CR : ".$id." is pending against you ."."Please take appropriate action"."\n\n\n"."Regards ,"."\n"."iVSE - WebChecklist Messenger"
."\n\n"."In case of concerns :"."\n"."mailto:gaurav.pruthi@tcs.com"."\n"."ESN : 3048");


//$log->logInfo('Code being hitted here[216]');
//Sending mail...
$m->sendmail();	
//*******************************End Mail Part**********************************************
}
  
//Redirect to mainpage after submission..
  
echo '<script type="text/javascript">','alert("Comments Successfully Submitted , redirecting to mainpage");',
	'window.location = "http://172.26.195.159/checklist/php/mainpage.php";' ,'</script>';  
?>